package com.tanhua.backstage.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.backstage.service.HtMessageService;
import com.tanhua.backstage.vo.HtPublishVo;
import com.tanhua.backstage.vo.HtPageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage/messages")
@Slf4j
public class HtMessageController {

    @Autowired
    private HtMessageService htMessageService;


    /**
     * 消息审核拒绝
     *
     * @param publishIds
     * @return
     */
    @PostMapping("reject")
    public ResponseEntity<String> ChangeStateToReject(@RequestBody List<String> publishIds){
        try {
            if(CollUtil.isEmpty(publishIds)){
                return ResponseEntity.ok(null);
            }
            Boolean result = this.htMessageService.ChangeStateToReject(publishIds);
            if(result){
                return ResponseEntity.ok("修改成功");
            }
        } catch (Exception e) {
           log.error("修改状态出错了~~~");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(null);
    }


    /**
     * 消息审核通过
     *
     * @param publishIds
     * @return
     */
    @PostMapping("pass")
    public ResponseEntity<String> ChangeStateToPass(@RequestBody List<String> publishIds){
        try {
            if(CollUtil.isEmpty(publishIds)){
                return ResponseEntity.ok(null);
            }
            Boolean result = this.htMessageService.ChangeStateToPass(publishIds);
            if(result){
                return ResponseEntity.ok("修改成功");
            }
        } catch (Exception e) {
            log.error("修改状态出错了~~~");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 消息审核撤回
     *
     * @param publishIds
     * @return
     */
    @PostMapping("revocation")
    public ResponseEntity<String> ChangeStateToRevocation(@RequestBody List<String> publishIds){
        try {
            if(CollUtil.isEmpty(publishIds)){
                return ResponseEntity.ok(null);
            }
            Boolean result = this.htMessageService.ChangeStateToRevocation(publishIds);
            if(result){
                return ResponseEntity.ok("修改成功");
            }
        } catch (Exception e) {
            log.error("修改状态出错了~~~");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 将选中的动态置顶
     *
     * @param publishId
     * @return
     */
    @PostMapping("{id}/top")
    public ResponseEntity<String> ToTopOfPublish(@PathVariable("id")String publishId){
        try {
            if(ObjectUtil.isEmpty(publishId)){
                return ResponseEntity.ok(null);
            }
            Boolean result = this.htMessageService.ToTopOfPublish(publishId);
            if (result){
                return ResponseEntity.ok("修改成功");
            }
        } catch (Exception e) {
            log.error("设置置顶失败~~~");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 将选中的动态取消置顶
     *
     * @param publishId
     * @return
     */
    @PostMapping("{id}/untop")
    public ResponseEntity<String> ToUnTopOfPublish(@PathVariable("id")String publishId){
        try {
            if(ObjectUtil.isEmpty(publishId)){
                return ResponseEntity.ok(null);
            }
            Boolean result = this.htMessageService.ToUnTopOfPublish(publishId);
            if (result){
                return ResponseEntity.ok("修改成功");
            }
        } catch (Exception e) {
            log.error("设置非置顶失败~~~");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查看单个动态的内容
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<HtPublishVo> queryPublish(@PathVariable ("id") String publishId){

        try {
            HtPublishVo htPublishVo = this.htMessageService.queryPublish(publishId);
            if (!ObjectUtil.isEmpty(htPublishVo)){
                return ResponseEntity.ok(htPublishVo);
            }
            return ResponseEntity.ok(htPublishVo);
        } catch (Exception e) {
            log.error("查看信息详细出错了~~~");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查询消息列表（全部，或者按照审核状态）
     * 也可以通过消息id单独查询，也可以根据发布时间查询，也可以再时间范围类查询id
     *
     * @param pagesize 分页参数
     * @param page 分页参数
     * @param sortProp 排序字段
     * @param sortOrder 排序方式：ascending 升序 descending 降序
     * @param id  消息id(动态id)
     * @param sd 开始时间
     * @param ed 结束时间
     * @param uid 当前登录者的id（后台管理员id）
     * @param state 审核状态（3，4，5--待审核，已拒绝，已通过）
     * @return 消息列表
     */
    @GetMapping
    public ResponseEntity<HtPageResult> showMessageList(@RequestParam(value = "pagesize",defaultValue = "10",required = false) Integer pagesize,
                                                        @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                                        @RequestParam(value = "sortProp")String sortProp,
                                                        @RequestParam(value = "sortOrder") String sortOrder,
                                                        @RequestParam(value = "id",required = false) String id,
                                                        @RequestParam(value = "sd",required = false)String sd,
                                                        @RequestParam(value = "ed",required = false)String ed,
                                                        @RequestParam(value = "uid",required = false)String uid,
                                                        @RequestParam(value = "state",required = false)String state){
        try {
            HtPageResult htPageResult = htMessageService.showMessageList(page, pagesize, sortProp, sortOrder, id, sd, ed, uid, state);
            return ResponseEntity.ok(htPageResult);
        } catch (Exception e) {
            log.error("查询消息列表出错");
        }
        return ResponseEntity.status(500).body(null);

    }

}
